/*
 *--------------------------------------------------------------------------
 *   Battlefield - A Realtime Network Multiplayer Game
 *   =======================================================
 *   Developed by Group D02 - 2009/2010 Semester 4 - CS2103
 *     Harry Nguyen Duy Hoang <nnduyhoang@yahoo.co.uk>
 *     Kent Chng Siang Rong <fivefootway@gmail.com>
 *     Lim Yong Peng <limpeng1986@gmail.com>
 *     Loh Xiankun <u0807185@nus.edu.sg>
 *   Instructed by
 *     Dr. Damith C.Rajapakse <damith@gmail.com>
 *   =======================================================
 *   $Id: RmiServer.java 568 2010-07-25 20:56:20Z Harry $
 *   $LastChangedDate: 2010-07-25 20:56:20 +0000 (Sun, 25 Jul 2010) $
 *   $LastChangedBy: Harry $
 *--------------------------------------------------------------------------
 */
package battlefield.net;

import battlefield.BattlefieldServerImp;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.HashSet;
import java.util.Properties;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author xiankun
 */
public class RmiServer {

    private static final String FACTORY = "java.naming.factory.initial";
    private static final String FACTORY_NAME = "com.sun.jndi.rmi.registry.RegistryContextFactory";
    private static final String PROVIDER = "java.naming.provider.url";
    private static String PROVIDER_URL = "rmi://";
    private static HashSet<String> allServerName;
    private String serverName;
    private Registry serverRegistry;
    private String ipAddress;
    private String portNumber = "1099";

    public RmiServer() {
        this.allServerName = new HashSet<String>();
    }

    public RmiServer(String serverName) {
        this.allServerName = new HashSet<String>();
        this.serverName = serverName;
    }

    //static functions
    public static void addServerName(String serverName) {
        allServerName.add(serverName);
    }

    public static boolean serverNameLookup(String serverName) {
        return allServerName.contains(serverName);
    }

    public static void removeServerName(String serverName) throws RmiServerException {
        if (serverNameLookup(serverName)) {
            allServerName.remove(serverName);
        } else {
            throw new RmiServerException("unable to find " + serverName);
        }
    }

    public String getIpAddress() {
        return ipAddress;
    }

    public void setIpAddress(String ipAddress) {
        this.ipAddress = ipAddress;
    }

    public String getPortNumber() {
        return portNumber;
    }

    public void setPortNumber(String portNumber) {
        this.portNumber = portNumber;
    }

    public void setServerName(String serverName) {
        this.serverName = serverName;
    }

    public String getServerName() {
        return this.serverName;
    }

    public void startServer() throws RemoteException, NamingException, UnknownHostException {
        try {
            serverRegistry = LocateRegistry.createRegistry(1099);
            System.out.println("registry started");
            //set ip address and port number
            InetAddress addr = InetAddress.getLocalHost();
            this.ipAddress = addr.getHostName();
            //all manager will be needed to create an instance here and rebind it into initialcontext with the class name
            BattlefieldServerImp battlefieldServer = new BattlefieldServerImp();
            Properties props = new Properties();
            props.put(RmiServer.FACTORY, RmiServer.FACTORY_NAME);
            props.put(RmiServer.PROVIDER, RmiServer.PROVIDER_URL + ipAddress + ":" + portNumber);
            InitialContext ic = new InitialContext(props);
            //bind the manager with the initialcontext "ic" here
            ic.rebind("BattlefieldLogic", battlefieldServer);
            System.out.println("BattleFieldServer Started");
        } catch (UnknownHostException ex) {
            throw new UnknownHostException(ex.getMessage());
        } catch (NamingException ex) {
            throw new NamingException(ex.getMessage());
        } catch (RemoteException ex) {
            throw new RemoteException(ex.getMessage());
        }
    }

    public void stopServer() {
        try {
            if (serverRegistry != null) {
                UnicastRemoteObject.unexportObject(serverRegistry, true);
                serverRegistry = null;
                System.out.println("BattleField Server Stopped");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
